<?php

/**
 * This is the model class for table "mdk_post".
 *
 * The followings are the available columns in table 'mdk_post':
 * @property integer $id
 * @property integer $author
 * @property string $title
 * @property string $description
 * @property string $url
 * @property string $type
 * @property string $thumb
 * @property string $createtime
 * @property string $updatetime
 */
class Post extends CActiveRecord
{
	const STATUS_DRAFT=1;
        const STATUS_PUBLISHED=2;
        const STATUS_ARCHIVED=3;
	/*
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Post the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'mdk_post';
	}
        
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, description, url, type, thumb', 'required'),
			array('type', 'in', 'range'=>array('video','photo','text')),
			array('title', 'length', 'max'=>150),
			array('thumb', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('title, description, type', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                        'authorData' => array(self::BELONGS_TO, 'Users', 'author'),
                        'comments' => array(self::HAS_MANY, 'Comments', 'id_post',
                            'condition'=>'comments.status='.Comments::STATUS_APPROVED,
                            'order'=>'comments.create_time DESC'),
                        'commentCount' => array(self::STAT, 'Comments', 'id_post',
                            'condition'=>'status='.Comments::STATUS_APPROVED),
                 );
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'author' => 'Автор',
			'title' => 'Заголовок',
			'description' => 'Описания',
			'url' => 'Ссылка видео',
			'type' => 'Тип записи',
			'thumb' => 'Превью',
                        'createtime' => 'Дата создания',
                        'updatetime' => 'Дата обновления',
		);
	}
	public function getUrl()
    {
        return Yii::app()->createUrl('post/view', array(
            'id'=>$this->id,
            'title'=>$this->title,
        ));
    }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('author',$this->author);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('thumb',$this->thumb,true);
                $criteria->compare('createtime',$this->createtime);
                $criteria->compare('updatetime',$this->updatetime);
                $criteria->compare('thumb',$this->thumb,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	protected function beforeSave()
	{
    	if(parent::beforeSave())
    	{
        	if($this->isNewRecord)
        	{
                    $this->createtime=$this->updatetime=time();
        	}
        	else
                    $this->updatetime=time();
        	return true;
    	}
    	else
        return false;
	}
}